<?php
namespace Shop\Controller;
use Think\Controller;

class CustomizedTravelController extends \Org\Util\AdminController {

    /**
     * model的中文名称
     */
    protected $modelName = '筹单';

    /**
     * model的icon
     */
    protected $modelIcon = 'icon-globe';

    /**
     * 权限验证标志位,默认false
     * false:所有action都需要权限验证;
     * true:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $notAuthority = false;


    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
        //扣钱
        $user = D('User')->find(session('商家')['id']);
        $newBalance = $user['balance']-C('BUY_FUNDING_PRICE');
        D('User')->where(['id'=>session('商家')['id']])->data(['balance'=>$newBalance])->save();
        //增加资金明细
        unset($data);
        $data['user_id'] = session('商家')['id'];
        $data['pay_type'] = '账户余额';
        $data['amount'] = '-'.C('BUY_FUNDING_PRICE');
        $data['remark'] = '购买筹单';
        $data['order_type'] = 'BuyFunding';
        $data['order_id'] = $id;
        D('Finance')->create($data);
        D('Finance')->add();
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {
    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        $user = D('User')->find(session('商家')['id']);
        if ($user['balance']<C('BUY_FUNDING_PRICE')) {
            $this->error('余额不足!');
        }

        $_POST['user_id'] = session('商家')['id'];
        $_POST['state'] = '待处理';
        $_POST['transaction_type'] = '购买';
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        parent::update($id);
    }

    public function list() {
        $_GET['searchMap']['transaction_type'] = '购买';
        $_GET['searchMap']['user_id'] = session('商家')['id'];
        parent::list();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }


    public function tableOpeationButton() {
        return '
            
            <if condition="$vo[\'state\'] eq \'待发起\'">
                <a href="__MODULE__/Funding/detail/id/{$vo[\'funding_id\']}">
                    <button class="btn btn-xs btn-info" style="border: 1px;">
                        <i class="icon-search" style="margin-right: 0px;"></i>
                    </button>
                </a>
                <a href="__MODULE__/Funding/update/id/{$vo[\'funding_id\']}" onclick="return confirm(\'发起后购买筹单的金额将进入旅社账户,是否确认发起筹单?\');">
                    <button class="btn btn-xs btn-info" style="border: 1px;">
                        <i class="icon-group" style="margin-right: 0px;"></i>
                    </button>
                </a>
            </if>
            <if condition="$vo[\'state\'] eq \'已完成\'">
                <a href="__ROOT__/Home/Index/call/id1/' . session('商家')['id'] . '/id2/{$vo[\'club_id\']}">
                    <button class="btn btn-xs btn-success" style="border: 1px;">
                        <i class="icon-phone" style="margin-right: 0px;"></i>
                    </button>
                </a>
                <a href="__MODULE__/Funding/detail/id/{$vo[\'funding_id\']}">
                    <button class="btn btn-xs btn-info" style="border: 1px;">
                        <i class="icon-search" style="margin-right: 0px;"></i>
                    </button>
                </a>
                <a href="__MODULE__/Funding/update/id/{$vo[\'funding_id\']}" >
                    <button class="btn btn-xs btn-info" style="border: 1px;">
                        <i class="icon-edit" style="margin-right: 0px;"></i>
                    </button>
                </a>
            </if>
            ';
    }

}